// pages/home/index.js
const {
  get,
  post
} = getApp();
Page({
  data: {
    showPage: false,
    params: {
      page: 1,
      pageSize: 10
    },
    dataList: [],
    bannerList:[],
    goodsCatList: [],
    hotList:[],
    showLoading: true
  },
  // 4个按钮跳转
  goToUrl: function (e) {
    let type = e.currentTarget.dataset.type;
    if(type=='name'){
      wx.navigateTo({
        url: '../selected/index',
      })
    }else{
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },
  // 详情页跳转
  goToCommodity: function (e) {
    wx.navigateTo({
      url: `../commodity/index?unique_id=${e.currentTarget.dataset.unique_id}`,
    });
  },
  // 页面初始化加载
  onLoad: function () {
    
  },
  // 获取爆款商品
  getHot(){
    get("/index/hot",{
      page: 1,
      pageSize: 3
    }).then(res => {
      if (res.result == 1) {
        this.setData({
          hotList:res.data
        });
        this.getList();
      };
    })
  },
  getData(){
    post("/index/banner").then(res => {
      if (res.result == 1) {
        this.setData({
          bannerList:res.data.bannerList,
          goodsCatList: res.data.goodsCatList
        });
        this.getHot();
      };
    })
  },
  // 设置tabBar
  onShow: function () {
    this.getData();
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    };
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    })
  },
  // 上拉加载
  onReachBottom: function () {
  },
  // 获取数据
  getList() {
    if (this.data.showLoading) {
      get("/index/selected",this.data.params).then(res => {
        if (res.result == 1) {
          this.setData({
            dataList:res.data,
            showPage: true
          });
        };
      })
    }
  }
})